from datetime import datetime

import requests

from agency import agencies


def get_acsrf_token(str_value):
    hash_value = 5381
    for char in str_value:
        hash_value = (hash_value << 5) + hash_value + ord(char)
    return hash_value & 0x7fffffff


def get_cookie(name):
    with open("cookie.txt", mode="r") as f:
        cookie = f.read()
    if name == "all":
        return cookie
    res = [item for item in cookie.split(";") if name in item.split("=")[0]]
    if len(res) == 0:
        print("获取失败")
        exit(0)
    return res[0].split("=")[1]


def get_industry_3(industry, item):
    result = industry[(industry['系统一级行业'] == item['industry_first_name']) & (industry['系统二级行业'] == item[
        'industry_second_name'])]
    if len(result) == 0:
        print("没查询到三级行业")
        exit(0)
    return result['KPI行业LV1'].iloc[0]


def get_k_frame_industry(agency_uid, second_industry_id):
    cookies = get_cookie('all')
    protect = get_cookie('gdt_protect')
    bm_id = get_cookie('bm_id')
    url = f"https://e.qq.com/agp/policy/get_k_frame_by_industry?bm_id={bm_id}&agency_uid={agency_uid}&second_industry_id={second_industry_id}&g_tk={get_acsrf_token(protect)}"
    res = requests.get(
        url,
        headers={
            "cookie": cookies,
        })
    return res.json()['data']['k_frame']


def get_k_frame_custom(agency_uid, company_name, second_industry_id):
    cookies = get_cookie('all')
    protect = get_cookie('gdt_protect')
    bm_id = get_cookie('bm_id')
    url = f"https://e.qq.com/agp/policy/get_k_frame_by_enterprise_and_industry?bm_id={bm_id}&agency_uid={agency_uid}&company_name={company_name}&second_industry_id={second_industry_id}&g_tk={get_acsrf_token(protect)}"
    res = requests.get(url, headers={"cookie": cookies})
    return res.json()['data']['k_frame']


def is_new_custom(agency_uid, company_name):
    cookies = get_cookie('all')
    protect = get_cookie('gdt_protect')
    bm_id = get_cookie('bm_id')
    url = f"https://e.qq.com/agp/policy/judge_new_advertiser?bm_id={bm_id}&agency_uid={agency_uid}&company_name={company_name}&g_tk={get_acsrf_token(protect)}"
    res = requests.get(url, headers={"cookie": cookies})
    data = res.json()['data']
    if data['is_new_advertiser'] == 1:
        return "是"
    else:
        return "否"


def judge_industry_subsidies_customer(agency_uid, company_name, industry_id):
    cookies = get_cookie('all')
    protect = get_cookie('gdt_protect')
    bm_id = get_cookie('bm_id')
    url = f"https://e.qq.com/agp/policy/judge_industry_subsidies_customer?bm_id={bm_id}&agency_uid={agency_uid}&company_name={company_name}&industry_id={industry_id}&g_tk={get_acsrf_token(protect)}"
    res = requests.get(url, headers={"cookie": cookies})
    return res.json()['data']['is_industry_subsidies_customer']


def get_subside(res):
    while True:
        print(f"运营补贴查询结果：{res}，请输入(1享受，2不享受)")
        subside = input()
        if subside == '1':
            print("您输入了'享受'")
            return True
        if subside == '2':
            print("您输入了'不享受'")
            return False


def get_season():
    now = datetime.now()
    # 获取年份
    year = now.year
    # 根据当前月份计算季度
    month = now.month
    if month in [1, 2, 3]:
        quarter = 'Q1'
    elif month in [4, 5, 6]:
        quarter = 'Q2'
    elif month in [7, 8, 9]:
        quarter = 'Q3'
    else:
        quarter = 'Q4'
    return f"{year}年{quarter}新增"


def get_back_id(agent_id):
    agency_list = [item for item in agencies if 'agency_uid' in item and str(item['agency_uid']) == str(agent_id)]
    if len(agency_list) == 0:
        return None
    else:
        return agency_list[0]


def get_k_frame_input(frame_ind, frame):
    while True:
        print(f"行业K框：{frame_ind}|客户K框：{frame}|请根据判断输入对应数值(1:K1|2:K2|3:K3|4:K4|5:K5|6:K6)：")
        res = input()
        if res == '1':
            return "K1"
        if res == '2':
            return "K2"
        if res == '3':
            return "K3"
        if res == '4':
            return "K4"
        if res == '5':
            return "K5"
        if res == '6':
            return "K6"
def get_input_values():
    print("请输入集团sku:")
    sku = input()
    print("请输入销售:")
    sale = input()
    print("请输入一级事业部:")
    first_cause_dept = input()
    print("请输入二级事业部:")
    second_cause_dept = input()
    print("请输入业绩来源分类:")
    source = input()
    print("请输入(1直客|2渠道):")
    zOrq = input()
    if zOrq == "1":
        zOrq = "直客"
    elif zOrq == "2":
        zOrq = "渠道"
    else:
        print("您输入的不是数字，请重新运行程序")
    print("请输入渠道/直客公司简称:")
    zOrq_simp = input()
    print("请输入运营类型(1客户运营|2渠道运营|3亦芯运营):")
    operate_type = input()
    if operate_type == "1":
        operate_type="客户运营"
    if operate_type == "2":
        operate_type = "渠道运营"
    if operate_type == "3":
        operate_type = "亦芯运营"
    print(f"您输入了{operate_type}")
    return sku, sale, first_cause_dept, second_cause_dept, source, zOrq, zOrq_simp, operate_type